/*     */ package com.zimbra.cs.account.accesscontrol;
/*     */ 
/*     */ import com.google.common.base.Objects;
/*     */ import com.google.common.base.Objects.ToStringHelper;
/*     */ import com.google.common.collect.ImmutableList;
/*     */ import com.google.common.collect.ImmutableSet;
/*     */ import com.google.common.collect.Sets;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AttributeClass;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.ldap.LdapProv;
/*     */ import com.zimbra.cs.ldap.IAttributes;
/*     */ import com.zimbra.cs.ldap.SearchLdapOptions.SearchLdapVisitor;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.Iterator;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class SearchGrants
/*     */ {
/*     */   private final Provisioning prov;
/*     */   private final Set<TargetType> targetTypes;
/*     */   private final Set<String> granteeIds;
/*     */   private final Account acct;
/*     */   private final Set<Right> rights;
/*     */   private final boolean onMaster;
/*  52 */   private final Set<String> fetchAttrs = Sets.newHashSet(new String[] { "cn", "zimbraId", "objectClass", "zimbraACE" });
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   SearchGrants(Provisioning prov, Set<TargetType> targetTypes, Set<String> granteeIds)
/*     */   {
/*  59 */     this.prov = prov;
/*  60 */     this.targetTypes = targetTypes;
/*  61 */     this.granteeIds = granteeIds;
/*  62 */     this.acct = null;
/*  63 */     this.rights = null;
/*  64 */     this.onMaster = true;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   SearchGrants(Provisioning prov, Set<TargetType> targetTypes, Account acct, Set<Right> rights, boolean onMaster)
/*     */   {
/*  72 */     this.prov = prov;
/*  73 */     this.targetTypes = targetTypes;
/*  74 */     this.granteeIds = null;
/*  75 */     this.acct = acct;
/*  76 */     this.rights = rights;
/*  77 */     this.onMaster = onMaster;
/*     */   }
/*     */   
/*     */   void addFetchAttribute(String attr) {
/*  81 */     this.fetchAttrs.add(attr);
/*     */   }
/*     */   
/*     */   void addFetchAttribute(Set<String> attrs) {
/*  85 */     this.fetchAttrs.addAll(attrs);
/*     */   }
/*     */   
/*     */   static final class GrantsOnTarget {
/*     */     private final Entry targetEntry;
/*     */     private final ZimbraACL acl;
/*     */     
/*     */     private GrantsOnTarget(Entry targetEntry, ZimbraACL acl) {
/*  93 */       this.targetEntry = targetEntry;
/*  94 */       this.acl = acl;
/*     */     }
/*     */     
/*     */     Entry getTargetEntry() {
/*  98 */       return this.targetEntry;
/*     */     }
/*     */     
/*     */     ZimbraACL getAcl() {
/* 102 */       return this.acl;
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   static final class SearchGrantsResults
/*     */   {
/*     */     private final Provisioning prov;
/*     */     
/*     */ 
/* 114 */     private final Map<String, SearchGrants.GrantsOnTargetRaw> rawResults = new HashMap();
/*     */     
/*     */     private Set<SearchGrants.GrantsOnTarget> results;
/*     */     
/*     */ 
/*     */     SearchGrantsResults(Provisioning prov)
/*     */     {
/* 121 */       this.prov = prov;
/*     */     }
/*     */     
/*     */     private void addResult(SearchGrants.GrantsOnTargetRaw result) {
/* 125 */       this.rawResults.put(result.getTargetId(), result);
/*     */     }
/*     */     
/*     */ 
/*     */     Set<SearchGrants.GrantsOnTarget> getResults()
/*     */       throws ServiceException
/*     */     {
/* 132 */       return getResults(false);
/*     */     }
/*     */     
/*     */     Set<SearchGrants.GrantsOnTarget> getResults(boolean needFullDL) throws ServiceException {
/* 136 */       if (this.results == null) {
/* 137 */         this.results = new HashSet();
/* 138 */         for (SearchGrants.GrantsOnTargetRaw grants : this.rawResults.values()) {
/* 139 */           this.results.add(getGrants(this.prov, grants, needFullDL));
/*     */         }
/*     */       }
/* 142 */       return this.results;
/*     */     }
/*     */     
/*     */ 
/*     */     private SearchGrants.GrantsOnTarget getGrants(Provisioning prov, SearchGrants.GrantsOnTargetRaw sgr, boolean needFullDL)
/*     */       throws ServiceException
/*     */     {
/*     */       TargetType tt;
/*     */       
/* 151 */       if (sgr.objectClass.contains(AttributeClass.OC_zimbraCalendarResource)) {
/* 152 */         tt = TargetType.calresource; } else { TargetType tt;
/* 153 */         if (sgr.objectClass.contains(AttributeClass.OC_zimbraAccount)) {
/* 154 */           tt = TargetType.account; } else { TargetType tt;
/* 155 */           if (sgr.objectClass.contains(AttributeClass.OC_zimbraCOS)) {
/* 156 */             tt = TargetType.cos; } else { TargetType tt;
/* 157 */             if (sgr.objectClass.contains(AttributeClass.OC_zimbraDistributionList)) {
/* 158 */               tt = TargetType.dl; } else { TargetType tt;
/* 159 */               if (sgr.objectClass.contains(AttributeClass.OC_zimbraGroup)) {
/* 160 */                 tt = TargetType.group; } else { TargetType tt;
/* 161 */                 if (sgr.objectClass.contains(AttributeClass.OC_zimbraDomain)) {
/* 162 */                   tt = TargetType.domain; } else { TargetType tt;
/* 163 */                   if (sgr.objectClass.contains(AttributeClass.OC_zimbraServer)) {
/* 164 */                     tt = TargetType.server; } else { TargetType tt;
/* 165 */                     if (sgr.objectClass.contains(AttributeClass.OC_zimbraAlwaysOnCluster)) {
/* 166 */                       tt = TargetType.alwaysoncluster; } else { TargetType tt;
/* 167 */                       if (sgr.objectClass.contains(AttributeClass.OC_zimbraUCService)) {
/* 168 */                         tt = TargetType.ucservice; } else { TargetType tt;
/* 169 */                         if (sgr.objectClass.contains(AttributeClass.OC_zimbraXMPPComponent)) {
/* 170 */                           tt = TargetType.xmppcomponent; } else { TargetType tt;
/* 171 */                           if (sgr.objectClass.contains(AttributeClass.OC_zimbraZimletEntry)) {
/* 172 */                             tt = TargetType.zimlet; } else { TargetType tt;
/* 173 */                             if (sgr.objectClass.contains(AttributeClass.OC_zimbraGlobalConfig)) {
/* 174 */                               tt = TargetType.config; } else { TargetType tt;
/* 175 */                               if (sgr.objectClass.contains(AttributeClass.OC_zimbraAclTarget)) {
/* 176 */                                 tt = TargetType.global;
/*     */                               } else
/* 178 */                                 throw ServiceException.FAILURE("cannot determine target type from SearchGrantResult. " + sgr, null);
/*     */                             } } } } } } } } } } } }
/*     */       TargetType tt;
/* 181 */       Entry entry = null;
/*     */       try {
/* 183 */         if (tt == TargetType.zimlet) {
/* 184 */           entry = TargetType.lookupTarget(prov, tt, TargetBy.name, sgr.cn);
/*     */         } else {
/* 186 */           entry = TargetType.lookupTarget(prov, tt, TargetBy.id, sgr.zimbraId, needFullDL, true);
/*     */         }
/* 188 */         if (entry == null) {
/* 189 */           ZimbraLog.acl.warn("canot find target by id %s", new Object[] { sgr.zimbraId });
/* 190 */           throw ServiceException.FAILURE("canot find target by id " + sgr.zimbraId + ". " + sgr, null);
/*     */         }
/* 192 */         ZimbraACL acl = new ZimbraACL(sgr.zimbraACE, tt, entry.getLabel());
/* 193 */         return new SearchGrants.GrantsOnTarget(entry, acl, null);
/*     */       } catch (ServiceException e) {
/* 195 */         throw ServiceException.FAILURE("canot find target by id " + sgr.zimbraId + ". " + sgr, null);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   private static class GrantsOnTargetRaw
/*     */   {
/*     */     private final String cn;
/*     */     private final String zimbraId;
/*     */     private final Set<String> objectClass;
/*     */     private final String[] zimbraACE;
/*     */     
/*     */     private GrantsOnTargetRaw(Map<String, Object> attrs)
/*     */     {
/* 210 */       this.cn = ((String)attrs.get("cn"));
/* 211 */       this.zimbraId = ((String)attrs.get("zimbraId"));
/* 212 */       this.objectClass = ImmutableSet.copyOf(getMultiAttrString(attrs, "objectClass"));
/* 213 */       this.zimbraACE = getMultiAttrString(attrs, "zimbraACE");
/*     */     }
/*     */     
/*     */     public String toString()
/*     */     {
/* 218 */       return Objects.toStringHelper(this).add("cn", this.cn).add("zimbraId", this.zimbraId).add("objectClass", this.objectClass).add("zimbraACE", ImmutableList.copyOf(this.zimbraACE)).toString();
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     private String[] getMultiAttrString(Map<String, Object> attrs, String attrName)
/*     */     {
/* 227 */       Object obj = attrs.get(attrName);
/* 228 */       if ((obj instanceof String)) {
/* 229 */         return new String[] { (String)obj };
/*     */       }
/* 231 */       return (String[])obj;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     private String getTargetId()
/*     */     {
/* 240 */       return this.zimbraId != null ? this.zimbraId : this.cn;
/*     */     }
/*     */   }
/*     */   
/*     */   private static class SearchGrantVisitor extends SearchLdapOptions.SearchLdapVisitor {
/*     */     private final SearchGrants.SearchGrantsResults results;
/*     */     
/*     */     SearchGrantVisitor(SearchGrants.SearchGrantsResults results) {
/* 248 */       this.results = results;
/*     */     }
/*     */     
/*     */     public void visit(String dn, Map<String, Object> attrs, IAttributes ldapAttrs)
/*     */     {
/* 253 */       SearchGrants.SearchGrantsResults.access$700(this.results, new SearchGrants.GrantsOnTargetRaw(attrs, null));
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */   SearchGrantsResults doSearch()
/*     */     throws ServiceException
/*     */   {
/* 261 */     Map<String, Set<String>> basesAndOcs = TargetType.getSearchBasesAndOCs(this.prov, this.targetTypes);
/* 262 */     SearchGrantsResults results = new SearchGrantsResults(this.prov);
/* 263 */     SearchGrantVisitor visitor = new SearchGrantVisitor(results);
/* 264 */     long start = 0L;
/* 265 */     if (ZimbraLog.acl.isTraceEnabled()) {
/* 266 */       start = System.currentTimeMillis();
/*     */     }
/* 268 */     for (Map.Entry<String, Set<String>> entry : basesAndOcs.entrySet()) {
/* 269 */       search((String)entry.getKey(), (Set)entry.getValue(), visitor);
/*     */     }
/* 271 */     if (ZimbraLog.acl.isTraceEnabled()) {
/* 272 */       ZimbraLog.acl.trace("SearchGrants.doSearch() %s", new Object[] { ZimbraLog.elapsedTime(start, System.currentTimeMillis()) });
/*     */     }
/* 274 */     return results;
/*     */   }
/*     */   
/*     */   private Set<String> getGranteeIds() throws ServiceException {
/* 278 */     if (this.granteeIds != null) {
/* 279 */       return this.granteeIds;
/*     */     }
/* 281 */     Set<String> ids = Sets.newHashSet(RightBearer.Grantee.getGrantee(this.acct, this.rights, false).getIdAndGroupIds());
/* 282 */     ids.add("00000000-0000-0000-0000-000000000000");
/* 283 */     ids.add("99999999-9999-9999-9999-999999999999");
/* 284 */     String domainId = this.acct.getDomainId();
/* 285 */     if (domainId != null) {
/* 286 */       ids.add(domainId);
/*     */     }
/*     */     
/* 289 */     return ids;
/*     */   }
/*     */   
/*     */   private void search(String base, Set<String> ocs, SearchGrantVisitor visitor)
/*     */     throws ServiceException
/*     */   {
/* 295 */     StringBuilder query = new StringBuilder("(&(|");
/* 296 */     for (String oc : ocs) {
/* 297 */       query.append('(').append("objectClass").append('=').append(oc).append(")");
/*     */     }
/* 299 */     query.append(")(|");
/*     */     Iterator i$;
/* 301 */     if (this.rights == null) {
/* 302 */       for (String granteeId : getGranteeIds()) {
/* 303 */         query.append('(').append("zimbraACE").append('=').append(granteeId).append("*)");
/*     */       }
/*     */     } else {
/* 306 */       for (i$ = getGranteeIds().iterator(); i$.hasNext();) { granteeId = (String)i$.next();
/* 307 */         for (Right right : this.rights)
/* 308 */           query.append('(').append("zimbraACE").append('=').append(granteeId).append("*").append(right.getName()).append(")");
/*     */       }
/*     */     }
/*     */     String granteeId;
/* 312 */     query.append("))");
/*     */     
/* 314 */     if (this.onMaster) {
/* 315 */       LdapProv.getInst().searchLdapOnMaster(base, query.toString(), (String[])this.fetchAttrs.toArray(new String[this.fetchAttrs.size()]), visitor);
/*     */     }
/*     */     else {
/* 318 */       LdapProv.getInst().searchLdapOnReplica(base, query.toString(), (String[])this.fetchAttrs.toArray(new String[this.fetchAttrs.size()]), visitor);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/accesscontrol/SearchGrants.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */